package net.oschina.app.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import net.oschina.app.R;
import net.oschina.app.base.ListBaseAdapter;
import net.oschina.app.util.StringUtils;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;

/**
 * Created by yihao on 9/18/15.
 */
public class DateSortedAdapter  extends ListBaseAdapter{

    public static interface DateItem{
        public Date getDate();
    }
    /**
     *
     * @param timeInterval seconds
     * @param sortMode sort mode
     */

    private int mTimeInterval;
    private int mSortMode;
    private Timer mTimer;
    //protected List<DateItem> mDateItems;
    protected List<DateItem> mSortedItems;
    private Date mStartTime;
    private WeakReference<Activity> mRefActivity;

    public DateSortedAdapter(Activity activity, int timeInterval, int sortMode){
        mTimer = new Timer();
        //mDateItems = new ArrayList<>();
        mSortedItems = new ArrayList<>();
        mRefActivity = new WeakReference<Activity>(activity);
        mTimeInterval = timeInterval;
        mSortMode = sortMode;
    }

    protected View getDateMarkView(int position, View convertView, ViewGroup parent)
    {
        if(mSortedItems.get(position) instanceof DateMark){
            DateMark mark = (DateMark) mSortedItems.get(position);
            return mark.getItemView(position, convertView, parent);
        }
        return null;
    }

    public int getDataSize() {
        return mSortedItems.size();
    }

    public void addDateItems(List<DateItem> dateItemses){
        if(dateItemses.size() > 0){
            mStartTime = dateItemses.get(dateItemses.size() - 1).getDate();
            for(int i=0; i<dateItemses.size(); i++) {
                DateItem item = dateItemses.get(dateItemses.size() - 1 - i);
                addDateItemImpl(item);
            }

            notifyDataSetChanged();
        }

    }

    public void addDateItem(DateItem item){
        addDateItemImpl(item);
        notifyDataSetChanged();
    }

    public int getViewTypeCount(){
        return 2;
    }

    public int getItemViewType(int position){
        if(mSortedItems.get(position) instanceof DateMark){
            return 1;
        }
        else{
            return 0;
        }
    }

//    private void sortItems(){
//
//        mSortedItems.clear();
//        Date date;
//        for(DateItem item:mDateItems){
//            date = item.getDate();
//            mSortedItems.add(item);
//            if((item.getDate().getTime() - date.getTime()) > mTimeInterval * 1000){
//
//            }
//        }
//
//    }

    private void addDateItemImpl(DateItem item){


        if((item.getDate().getTime() - mStartTime.getTime()) > mTimeInterval * 1000){
            mStartTime = item.getDate();
            mSortedItems.add(new DateMark(item.getDate()));
        }
        mSortedItems.add(item);

    }
//    private  class SortTask extends TimerTask{
//
//        @Override
//        public void run() {
//            sortItems();
//        }
//    }
//
    private class DateMark implements DateItem{

        private  Date mDate;

        public DateMark(Date date){
            mDate = date;
        }
        @Override
        public Date getDate() {
            return mDate;
        }

        public View getItemView(int position, View convertView, ViewGroup parent){
            DateMarkViewHolder viewHolder;
            if(null == convertView){
                convertView = mRefActivity.get().getLayoutInflater().inflate(R.layout.item_date_mark, null);
                viewHolder = new DateMarkViewHolder();
                viewHolder.mTvDate = (TextView) convertView.findViewById(R.id.textView_date);
                convertView.setTag(viewHolder);
            }
            viewHolder = (DateMarkViewHolder) convertView.getTag();
            viewHolder.mTvDate.setText(StringUtils.friendly_time(StringUtils.getDateString(mDate)));
            return convertView;
        }
    }

    private class DateMarkViewHolder {
        TextView mTvDate;

    }
}
